package handler

import (
	"admin-nexus-backend/internal/dto"
	"admin-nexus-backend/internal/service"
	"admin-nexus-backend/pkg/response"
	"strconv"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// OrganizationHandler 组织管理处理器
type OrganizationHandler struct {
	organizationService *service.OrganizationService
}

// NewOrganizationHandler 创建组织管理处理器
func NewOrganizationHandler(db *gorm.DB) *OrganizationHandler {
	return &OrganizationHandler{
		organizationService: service.NewOrganizationService(db),
	}
}

// GetOrganizationList 获取组织列表
func (h *OrganizationHandler) GetOrganizationList(c *gin.Context) {
	// 获取分页参数
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	// 获取组织列表
	organizations, total, err := h.organizationService.GetOrganizationList(page, pageSize)
	if err != nil {
		response.InternalServerError(c, "获取组织列表失败: "+err.Error())
		return
	}

	response.SuccessWithData(c, gin.H{
		"list":  organizations,
		"total": total,
		"page":  page,
		"size":  pageSize,
	})
}

// GetOrganizationByID 根据ID获取组织
func (h *OrganizationHandler) GetOrganizationByID(c *gin.Context) {
	// 获取组织ID
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "无效的组织ID")
		return
	}

	// 获取组织信息
	organization, err := h.organizationService.GetOrganizationByID(uint(id))
	if err != nil {
		response.NotFound(c, "组织不存在")
		return
	}

	response.SuccessWithData(c, organization)
}

// CreateOrganization 创建组织
func (h *OrganizationHandler) CreateOrganization(c *gin.Context) {
	// 绑定请求参数
	var req dto.CreateOrganizationRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "参数错误: "+err.Error())
		return
	}

	// 创建组织
	organization, err := h.organizationService.CreateOrganization(req)
	if err != nil {
		response.InternalServerError(c, "创建组织失败: "+err.Error())
		return
	}

	response.SuccessWithData(c, organization)
}

// UpdateOrganization 更新组织
func (h *OrganizationHandler) UpdateOrganization(c *gin.Context) {
	// 获取组织ID
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "无效的组织ID")
		return
	}

	// 绑定请求参数
	var req dto.UpdateOrganizationRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "参数错误: "+err.Error())
		return
	}

	// 更新组织
	organization, err := h.organizationService.UpdateOrganization(uint(id), req)
	if err != nil {
		response.InternalServerError(c, "更新组织失败: "+err.Error())
		return
	}

	response.SuccessWithData(c, organization)
}

// DeleteOrganization 删除组织
func (h *OrganizationHandler) DeleteOrganization(c *gin.Context) {
	// 获取组织ID
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		response.BadRequest(c, "无效的组织ID")
		return
	}

	// 删除组织
	err = h.organizationService.DeleteOrganization(uint(id))
	if err != nil {
		response.BadRequest(c, err.Error())
		return
	}

	response.SuccessWithMessage(c, "删除成功")
}

// GetOrganizationTree 获取组织树
func (h *OrganizationHandler) GetOrganizationTree(c *gin.Context) {
	// 获取组织树
	tree, err := h.organizationService.BuildOrganizationTree()
	if err != nil {
		response.InternalServerError(c, "获取组织树失败: "+err.Error())
		return
	}

	response.SuccessWithData(c, tree)
}
